package com.albertyjz.yygh.hosp.service.impl;

import com.albertyjz.yygh.hosp.repository.DepartmentRepository;
import com.albertyjz.yygh.hosp.service.DepartmentService;
import com.albertyjz.yygh.model.hosp.Department;
import com.albertyjz.yygh.model.hosp.Hospital;
import com.albertyjz.yygh.vo.hosp.DepartmentVo;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentRepository departmentRepository;

    @Override
    public void save(Map<String, Object> stringObjectMap) {
        String mapString= JSONObject.toJSONString(stringObjectMap);
        Department department = JSONObject.parseObject(mapString, Department.class);
        //根据科室编号和医院编号，判断是否有这个数据
        Department departmentExist=
                departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(),department.getDepcode());
        if(departmentExist != null) {
            departmentExist.setUpdateTime(new Date());
            departmentExist.setIsDeleted(0);
            departmentRepository.save(departmentExist);
        }else {
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }
    }

    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        List<DepartmentVo> result=new ArrayList<>();
        Department department=new Department();
        department.setHoscode(hoscode);
        Example example=Example.of(department);
        //所有科室列表信息
        List<Department> departmentList = departmentRepository.findAll(example);
        //根据大科室编号bigcode分组，获取每个大科室的下面的子科室
        Map<String, List<Department>> departmentMap =
                departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        //分组后进行遍历封装
        for(Map.Entry<String,List<Department>> entry : departmentMap.entrySet()){
            String bigcode=entry.getKey();
            List<Department> departmentList1=entry.getValue();
            //封装大科室
            DepartmentVo departmentVo=new DepartmentVo();
            departmentVo.setDepcode(bigcode);
            departmentVo.setDepname(departmentList1.get(0).getBigname());
            //封装小科室
            List<DepartmentVo> child=new ArrayList<>();
            for(Department depart:departmentList1){
                DepartmentVo departmentVo1= new DepartmentVo();
                departmentVo1.setDepcode(depart.getDepcode());
                departmentVo1.setDepname(depart.getDepname());
                child.add(departmentVo1);
            }
            departmentVo.setChildren(child);
            result.add(departmentVo);
        }
        return result;
    }
}
